package com.bootdo.system.controller;

import cn.hutool.core.date.DateUtil;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import com.bootdo.system.domain.ItemDO;
import com.bootdo.system.domain.SafeItemDO;
import com.bootdo.system.service.SafeItemService;
import com.bootdo.system.vo.SafeItemVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 安全检查项目
 * 
 * @author huizhang
 * @email 
 * @date 2021-01-25 17:10:14
 */
 
@Controller
@RequestMapping("/system/safeItem")
@Slf4j
public class SafeItemController {
	@Autowired
	private SafeItemService itemService;
	
	@GetMapping()
	@RequiresPermissions("system:safeItem:item")
	String Item(){
	    return "system/safeItem/item";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("system:safeItem:item")
	public List<SafeItemDO> list() {
		Map<String, Object> query = new HashMap<>(16);
		try {
			return itemService.list(query);
		} catch (Exception e) {
			log.error("出错：", e);
			return new ArrayList<>();
		}
	}
	
	@GetMapping("/add")
	@RequiresPermissions("system:safeItem:add")
	String add(){
	    return "system/safeItem/add";
	}

	@GetMapping("/add/{pId}")
	@RequiresPermissions("system:safeItem:add")
	String add(@PathVariable("pId") Long pId, Model model) {
		model.addAttribute("pId", pId);
		String parentName = null;
		try {
			parentName = itemService.get(pId).getSafePosition();
		} catch (Exception e) {
			log.error("根据parentId获取项目出错：", e);
			parentName = "";
		}
		if (pId == 0) {
			model.addAttribute("pName", "总楼栋");
		} else {
			model.addAttribute("pName", parentName);
		}
		return "system/safeItem/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("system:safeItem:edit")
	String edit(@PathVariable("id") Long id,Model model){
		SafeItemDO item = itemService.get(id);
		SafeItemVO itemVO = new SafeItemVO();
		BeanUtils.copyProperties(item, itemVO);
		itemVO.setCheckTimeStart(DateUtil.format(item.getCheckTimeStart(), "yyyy-MM-dd"));
		itemVO.setCheckTimeEnd(DateUtil.format(item.getCheckTimeEnd(), "yyyy-MM-dd"));
		model.addAttribute("item", itemVO);
		model.addAttribute("parentItemName", itemVO.getSafePosition());
	    return "system/safeItem/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("system:safeItem:add")
	public R save( SafeItemDO item){
		if(StringUtils.equals("2",item.getDelFlag())) {
			item.setName(item.getSafePosition());
		}
		if(itemService.save(item)>0){
			return R.ok();
		}
		return R.error();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("system:safeItem:edit")
	public R update( SafeItemDO item){
		if(StringUtils.equals("2",item.getDelFlag())) {
			item.setName(item.getSafePosition());
		}else {
			item.setName("");
		}
		itemService.update(item);
		return R.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("system:safeItem:remove")
	public R remove(Long itemId){
		if(itemService.remove(itemId)>0){
		return R.ok();
		}
		return R.error();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("system:safeItem:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids){
		itemService.batchRemove(ids);
		return R.ok();
	}
	
}
